<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item
        label="标题"
        prop="title"
      >
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="知识分类"
        prop="classifyId"
      >
      <treeselect
          style="width:215px"
          v-model="queryParams.classifyId"
          :options="classifyList"
          :normalizer="normalizer"
          placeholder="请选择所属分类"
        />
        <!-- <el-select
          v-model="queryParams.classifyId"
          placeholder="请选择知识分类"
          clearable
        >
          <el-option
            v-for="item in classifyList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select> -->
      </el-form-item>
      <el-form-item
        label="标签"
        prop="tagId"
      >
        <el-select
          v-model="queryParams.tagIdArr"
          placeholder="请选择标签"
          clearable
          multiple
        >
          <el-option
          v-for="item in dict.type.information_tag"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        label="关键字"
        prop="keyword"
      >
      <el-input
          v-model="queryParams.keyword"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
    </el-form>

    <el-row
      :gutter="10"
      class="mb8"
    >
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="articleList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column
        label="标题"
        align="center"
        prop="title"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="知识分类"
        align="center"
        prop="classifyName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="标签"
        align="center"
        prop="tagId"
        :show-overflow-tooltip="true"
      >
        <template v-slot="scope">
          <dict-tag
            :options="dict.type.information_tag"
            :value="scope.row.tagId ? scope.row.tagId.split(',') : []"
          />
        </template>
      </el-table-column>

      <!-- <el-table-column
        label="显示状态"
        align="center"
        prop="status"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.show_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column> -->
      <el-table-column
        label="缩略图"
        align="center"
        prop="image"
        width="100"
      >
        <template v-slot="scope">
          <image-preview
            :src="scope.row.image"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="排序"
        align="center"
        prop="sort"
      />
      <el-table-column
        label="描述"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="关键字"
        align="center"
        prop="keyword"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="作者"
        align="center"
        prop="author"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['information:article:edit']"
          >查看</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['information:article:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改知识文章对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="780px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-row>

          <el-col :span="12">
            <el-form-item
              label="标题"
              prop="title"
            >
              <el-input
                v-model="form.title"
                placeholder="请输入标题"
                maxlength="20"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="知识分类"
              prop="classifyId"
            >
              <!-- <el-select
                v-model="form.classifyId"
                placeholder="请选择知识分类"
                style="width:100%"
                disabled
              >
                <el-option
                  v-for="item in classifyList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select> -->
              <treeselect
                style="width:215px"
                v-model="form.classifyId"
                :options="classifyList"
                :normalizer="normalizer"
                placeholder="请选择知识分类"
              />
              <!-- <el-input
                v-model="form.classifyAncestors"
                type="textarea"
                placeholder="请输入内容"
              /> -->
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="标签"
              prop="tagId"
            >
              <el-checkbox-group v-model="form.tagId" disabled>
                <el-checkbox
                  v-for="dict in dict.type.information_tag"
                  :key="dict.value"
                  :label="dict.value"
                >
                  {{dict.label}}
                </el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="显示状态"
              prop="status"
            >
              <el-radio-group v-model="form.status" disabled>
                <el-radio
                  v-for="dict in dict.type.show_status"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item
              label="排序"
              prop="sort"
            >
              <el-input-number
              disabled
                v-model="form.sort"
                placeholder="请输入排序"
                controls-position="right"
                :min="0"
                :max="100"
                style="width:100%"
              />

            </el-form-item>
          </el-col>

          <el-col
            :span="24"
            id="image"
          >
            <el-form-item
              label="缩略图"
              prop="image"
            >
              <!-- <image-upload
              disabled
                v-model="form.image"
                :limit="1"
              /> -->
              <image-preview
            :src="form.image"
            :width="146"
            :height="146"
          />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item
              label="关键字"
              prop="keyword"
            >
              <el-input
              disabled
                v-model="form.keyword"
                placeholder="请输入关键字"
                maxlength="50"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="作者"
              prop="author"
            >
              <el-input
              disabled
                v-model="form.author"
                placeholder="请输入作者"
                maxlength="50"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="附件"
              prop="attachment"
            >
              <file-preview
                v-model="form.attachment"
                :fileSize="20"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="描述"
              prop="remark"
            >
              <el-input
               disabled
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
                rows="9"
                maxlength="500"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容">
              <editor-preview
                v-model="form.content"
                :min-height="192"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <!-- <el-button
          type="primary"
          @click="submitForm"
        >确 定</el-button> -->
        <el-button @click="cancel" icon="el-icon-close">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getAllListArticle,
  getArticle
} from "@/api/information/article";
import { listClassify } from "@/api/information/classify";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Article",
  dicts: ["information_tag", "show_status"],
  components: {
    Treeselect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 知识文章表格数据
      articleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        classifyId: undefined,
        status: undefined,
        title: null,
        tagIdArr: [],
        tagId:''
      },
      // 表单参数
      form: {
        status: "0",
      },
      // 表单校验
      rules: {
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        classifyId: [
          { required: true, message: "请选择知识分类", trigger: "blur" },
        ],
        publishTime: [
          { required: true, message: "请选择发布时间", trigger: "blur" },
        ],
        source: [{ required: true, message: "请选择来源", trigger: "blur" }],
      },
      classifyList: [],
    };
  },
  created() {
    this.getList();
    this.getClassifyList();
  },
  methods: {
    getClassifyList() {
      this.loading = true;
      listClassify({ status: 0 }).then((response) => {
        this.classifyList = this.handleTree(response.data, "id", "parentId");
      });
    },
     /** 转换知识分类数据结构 */
     normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children,
      };
    },
    /** 查询知识文章列表 */
    getList() {
      this.loading = true;
      getAllListArticle(this.queryParams).then((response) => {
        this.articleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        classifyId: null,
        tagId: [],
        title: null,
        status: "0",
        sort: null,
        remark: null,
        image: null,
        keyword: null,
        author: null,
        content: null,
        attachment: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      console.log(this.queryParams, 'this.queryParams')
      if (this.queryParams.tagIdArr.length > 0) {
        this.queryParams.tagId = this.queryParams.tagIdArr.join();
      }
      console.log(this.queryParams, 'this.queryParams后面')
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.status = undefined;
      this.queryParams.tagIdArr = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加知识文章";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getArticle(id).then((response) => {
        this.form = response.data;
        this.form.tagId = this.form.tagId.split(",");
        this.open = true;
        this.title = "查看知识搜索";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.tagId = this.form.tagId.join(",");
          if (this.form.id != null) {
            updateArticle(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArticle(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除知识文章编号为"' + ids + '"的数据项？')
        .then(function () {
          return delArticle(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "information/article/export",
        {
          ...this.queryParams,
        },
        `article_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
#image {
  max-height: 213px;
}
</style>
